package geppetto.cat.evaluate;

import geppetto.cat.alignments.AlignmentEvaluator;
import geppetto.cat.alignments.AlignmentSymetrization;
import geppetto.cat.alignments.AlignmentsSet;
import geppetto.cat.corpus.BilingualCorpus;
import geppetto.cat.models.AbstractModel;

public class EvaluateRareWords {

	String name;

	AbstractModel forward;

	AbstractModel backward;

	// 1st letter u/r: unregularized vs regularized
	// 2nd letter v/p: viterbi vs posterior
	// 3rd letter f/b/i: forward vs backward vs intersection
	AlignmentsSet align_uvf, align_uvb, align_uvi, align_uvu, align_uvg,
			align_uvgf, align_upf, align_upb, align_upi, align_upu, align_upg,
			align_upgf, align_rvf, align_rvb, align_rvi, align_rvu, align_rvg,
			align_rvgf, align_rpf, align_rpb, align_rpi, align_rpu, align_rpg,
			align_rpgf, align_ppf, align_ppb, align_ppi, align_ppu, align_ppg,
			align_ppgf, // post-pts
			align_prf, align_prb, align_pri, align_pru, align_prg, align_prgf; // post-
																				// recall

	// With Regularization

	public EvaluateRareWords(String mName, AbstractModel forward,
			AbstractModel backward, boolean processPosteriors,
			BilingualCorpus corpus, BilingualCorpus Rcorpus) {
		this(mName, forward, backward, processPosteriors, true, corpus, Rcorpus);
	}

	public EvaluateRareWords(String mName, AbstractModel forward,
			AbstractModel backward, boolean processPosteriors,
			boolean printResults, BilingualCorpus corpus,
			BilingualCorpus Rcorpus) {
		this.forward = forward;
		this.backward = backward;
		float threshold;
		java.text.DecimalFormat fmt = new java.text.DecimalFormat();
		fmt.applyPattern("0E0");

		AlignmentsSet gf = forward.getCorpus().getGold();
		AlignmentsSet gb = backward.getCorpus().getGold();
		// IntersectionModels inter = new IntersectionModels(forward,backward);

		String p = "    "
				+ mName
				+ " "
				+ fmt
						.format(forward.getCorpus()
								.getNumberOfTrainingSentences()) + " ";


		align_uvf = forward.viterbiAlignments(BilingualCorpus.TEST_CORPUS);
		align_uvb = backward.viterbiAlignments(BilingualCorpus.TEST_CORPUS);
		align_uvi = AlignmentSymetrization.intersect(align_uvf, align_uvb);
		align_uvu = AlignmentSymetrization.union(align_uvf, align_uvb);
		align_uvg = AlignmentSymetrization.growDiagAux(align_uvf, align_uvb,
				align_uvi, align_uvu);
		align_uvgf = AlignmentSymetrization.growDiagFinalAux(align_uvf,
				align_uvb, align_uvi, align_uvu, align_uvg);
		threshold = forward.tuneTreshholdAER(BilingualCorpus.DEV_CORPUS,false);
		align_upf = forward.posteriorAlignments(BilingualCorpus.TEST_CORPUS,
				threshold, false,false);
		threshold = backward.tuneTreshholdAER(BilingualCorpus.DEV_CORPUS, false);
		align_upb = backward.posteriorAlignments(BilingualCorpus.TEST_CORPUS,
				threshold, false,false);
		align_upi = AlignmentSymetrization.intersect(align_upf, align_upb);
		align_upu = AlignmentSymetrization.union(align_upf, align_upb);
		align_upg = AlignmentSymetrization.growDiagAux(align_upf, align_upb,
				align_upi, align_upu);
		align_upgf = AlignmentSymetrization.growDiagFinalAux(align_upf,
				align_upb, align_upi, align_upu, align_upg);

		if (printResults) {
			EvaluateRareWords.printResults(p + "Norm Viter Forw ", gf,
					align_uvf, corpus);
			EvaluateRareWords.printResults(p + "Norm Viter Back ", gb,
					align_uvb, Rcorpus);
			EvaluateRareWords.printResults(p + "Norm Viter Intr ", gf,
					align_uvi, corpus);
			EvaluateRareWords.printResults(p + "Norm Viter Union ", gf,
					align_uvu, corpus);
			EvaluateRareWords.printResults(p + "Norm Viter Grow ", gf,
					align_uvg, corpus);
			EvaluateRareWords.printResults(p + "Norm Viter Grow ", gf,
					align_uvgf, corpus);
			EvaluateRareWords.printResults(p + "Norm Poste Forw ", gf,
					align_upf, corpus);
			EvaluateRareWords.printResults(p + "Norm Poste Back ", gb,
					align_upb, Rcorpus);
			EvaluateRareWords.printResults(p + "Norm Poste Intr ", gf,
					align_upi, corpus);
			EvaluateRareWords.printResults(p + "Norm Poste Union ", gf,
					align_upu, corpus);
			EvaluateRareWords.printResults(p + "Norm Poste Grow ", gf,
					align_upg, corpus);
			EvaluateRareWords.printResults(p + "Norm Poste Grow ", gf,
					align_upgf, corpus);
		}
	}

	static void printResults(String name, AlignmentsSet gold,
			AlignmentsSet guess, BilingualCorpus corpus) {
		AlignmentEvaluator.Evaluation[] res = AlignmentEvaluator
				.evaluateRareWords(guess, gold, corpus);
		AlignmentEvaluator.Evaluation resSimple = AlignmentEvaluator.evaluate(
				guess, gold);
		System.out.println(name + " All " + resSimple);
		System.out.println(name + " rare " + res[0]);
		System.out.println(name + " non-rare " + res[1]);
	}

}
